package com.sky.mq.consumer;

import com.sky.constant.ESConstant;
import com.sky.event.DishEvent;
import com.sky.message.DishMessage;
import com.sky.service.es.DishServiceForES;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.rest.RestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;


@Slf4j
@Component
public class DishConsumer implements ApplicationListener<DishEvent> {

    @Autowired
    private DishServiceForES dishServiceForES;

    @Override
    @Async
    public void onApplicationEvent(DishEvent event) {
        try{
            DishMessage source = (DishMessage) event.getSource();
            if (Objects.equals(source.getType(), ESConstant.INSERT_ES)) {
                RestStatus status = dishServiceForES.insert(source.getId(), source.getName());
            } else if (Objects.equals(source.getType(), ESConstant.UPDATE_ES)) {
                RestStatus status = dishServiceForES.updateById(source.getId(), source.getName());
            } else if (Objects.equals(source.getType(), ESConstant.DELETE_ES)) {
                RestStatus status = dishServiceForES.deleteById(source.getId());
            }
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

//    public List<ESDish> query(DishMessage message){
//
//        return null;
//    }

//    public RestStatus insert(DishMessage message) throws IOException {
//        ESDish esDish = new ESDish(message.getId(), message.getName());
//        IndexRequest request = new IndexRequest("dish");
//        request.id(""+esDish.getId());
//        request.timeout(TimeValue.timeValueMillis(2000));
//        request.source(JSON.toJSONString(esDish), XContentType.JSON);
//        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
//        log.info("add status:"+response.status());
//        log.info("document id: "+response.getId());
//        log.info("response:"+response);
//        return response.status();
//    }
//
//    public RestStatus delete(DishMessage message) throws IOException {
//        DeleteRequest deleteRequest = new DeleteRequest("dish", "" + message.getId());
//        deleteRequest.timeout("1s");
//        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
//        return deleteResponse.status();
//    }
//
//    public RestStatus update(DishMessage message) throws IOException {
//        UpdateRequest updateRequest = new UpdateRequest("dish", "" + message.getId());
//        ESDish esDish = new ESDish(message.getId(), message.getName());
//        updateRequest.doc(JSON.toJSONString(esDish),XContentType.JSON);
//        UpdateResponse response = client.update(updateRequest,RequestOptions.DEFAULT);
//        log.info("status: "+response.status());
//        return response.status();
//    }

}
